package cz.muni.fi.pb138.xml.wrda.servise;

import cz.muni.fi.pb138.xml.wrda.core.XSD;
import cz.muni.fi.pb138.xml.wrda.dao.XSDDao;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author Ondřej Chrastina 430596@mail.muni.cz
 */
public class XSDServiceImpl implements XSDService {

    @Autowired
    private XSDDao xsdDao;

    @Override
    public List<XSD> getAllEntries() {
        return xsdDao.getAllEntries();
    }

    @Override
    public Object getAllEntriesDistinctNames() {
        return xsdDao.getAllEntriesDistinctNames();
    }

    @Override
    public void save(XSD artifact) {
        xsdDao.save(artifact);
    }

    @Override
    public String printAll() {
        return xsdDao.printAll();
    }

    @Override
    public boolean saveFile(InputStream file, String originalFileName) {
        XSD xsdEntry = new XSD();

        try {
            xsdEntry.setContent(new SAXBuilder().build(file));
        } catch (JDOMException ex) {
            Logger.getLogger(XSDServiceImpl.class.getName()).log(Level.SEVERE, "Error in file format", ex);
            return false;
        } catch (IOException ex) {
            Logger.getLogger(XSDServiceImpl.class.getName()).log(Level.SEVERE, "IO error in creating Document(jdom2) from file", ex);
            return false;
        }

        xsdEntry.setFileName(originalFileName);
        xsdEntry.parseElementsFromDocument();
        //Tagy se budou plnit i simple i complex type
        xsdEntry.parseSimpleTypesFromDocument();
        xsdEntry.parseComplexTypesFromDocument();

        xsdDao.save(xsdEntry);
        return true;
    }

    @Override
    public Object getAllEntriesByFileName(String fileName) {
        return xsdDao.getAllEntriesByFileName(fileName);
    }

    @Override
    public Object getEntryByFileNameAndTimeStamp(String name, String timeStamp) {
        return xsdDao.getEntryByFileNameAndTimeStamp(name, timeStamp);
    }

    @Override
    public void deleteAllVersionsXsdEntryByName(String fileName) {
        xsdDao.deleteAllVersionsXsdEntryByName(fileName);
    }

    @Override
    public void deleteXsdEntryTimeStamp(String timeStamp) {
        xsdDao.deleteXsdEntryTimeStamp(timeStamp);
    }

    @Override
    public StringReader getXSDContentAsInputStream(String timeStamp) {
        XSD entry = xsdDao.getEntryByTimestamp(timeStamp);
        XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
        StringReader reader  = new StringReader(out.outputString(entry.getContent()));
        return reader;
    }

    @Override
    public Object getAllEntriesDistinctNamesByFileName(String fileNameInput) {
        return xsdDao.getAllEntriesDistinctNamesByFileName(fileNameInput);
    }

    @Override
    public Object getAllEntriesDistinctNamesBySimpleTypeContains(String typeNameInput) {
        return xsdDao.getAllEntriesDistinctNamesBySimpleTypeContains(typeNameInput);
    }

    @Override
    public Object getAllEntriesDistinctNamesByComplexTypeContains(String typeNameInput) {
        return xsdDao.getAllEntriesDistinctNamesByComplexTypeContains(typeNameInput);
    }

    @Override
    public Object getAllEntriesDistinctNamesByFileNameAndType(String fileNameInput, String typeNameInput, String searchTypeSelect) {
       if("simple".equals(searchTypeSelect)){
           return xsdDao.getAllEntriesDistinctNamesByFileNameAndSimpleType(fileNameInput, typeNameInput);
       } 
       else{
           return xsdDao.getAllEntriesDistinctNamesByFileNameAndComplexType(fileNameInput, typeNameInput);
       }
       
    }

    @Override
    public Object getAllEntriesByFileNameAndSimpleTypeContains(String fileName, String typeNameInput) {
        return xsdDao.getAllEntriesByFileNameAndSimpleTypeContains(fileName, typeNameInput);
    }

    @Override
    public Object getAllEntriesByFileNameAndComplexTypeContains(String fileName, String typeNameInput) {
        return xsdDao.getAllEntriesByFileNameAndComplexTypeContains(fileName, typeNameInput);
    }
}
